package com.ztesoft.web.filecommon.db.dao;

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.ztesoft.core.common.Page;
import com.ztesoft.framework.exception.SysRuntimeException;
import com.ztesoft.framework.util.StringUtils;
import com.ztesoft.web.filecommon.db.arg.XpathVariantRelArg;
import com.ztesoft.web.filecommon.db.arg.XpathVariantRelArg.XpathVariantRelCriteria;
import com.ztesoft.web.filecommon.db.dao.mapper.IXpathVariantRelMapper;
import com.ztesoft.web.filecommon.db.po.XpathVariantRelPO;

@Repository
public class XpathVariantRelDao extends SqlSessionDaoSupport {

	@Resource(name = "majorSqlSessionTemplate")
	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		super.setSqlSessionTemplate(sqlSessionTemplate);
	}

	public List<Integer> countByArg(XpathVariantRelArg arg) {
		return getMapper().countByArg(arg);
	}

	public int deleteByArg(XpathVariantRelArg arg) {
		return getMapper().deleteByArg(arg);
	}

	public int updataTo00A(XpathVariantRelArg arg) {
		return getMapper().updataTo00A(arg);
	}

	public List<XpathVariantRelPO> selectByArg(XpathVariantRelArg arg) {
		return getMapper().selectByArg(arg);
	}

	public int updateByArgSelective(XpathVariantRelPO record, XpathVariantRelArg arg) {
		return getMapper().updateByArgSelective(record, arg);
	}

	public int updateByArg(XpathVariantRelPO record, XpathVariantRelArg arg) {
		return getMapper().updateByArg(record, arg);
	}

	public Page<XpathVariantRelPO> selectByArgAndPage(XpathVariantRelArg arg, Page<XpathVariantRelPO> resultPage) {
		List<XpathVariantRelPO> resultList = getMapper().selectByArgAndPage(arg, resultPage);
		resultPage.setResultList(resultList);
		return resultPage;
	}

	public int insert(XpathVariantRelPO record) {
		return getMapper().insert(record);
	}

	public int insertSelective(XpathVariantRelPO record) {
		return getMapper().insertSelective(record);
	}

	public int insertBatch(List<XpathVariantRelPO> records) {
		return getMapper().insertBatch(records);
	}

	public int deleteByPrimaryKey(Integer key) {
		return getMapper().deleteByPrimaryKey(key);
	}

	public XpathVariantRelPO selectByPrimaryKey(Integer key) {
		return getMapper().selectByPrimaryKey(key);
	}

	public List<XpathVariantRelPO> selectPOByDirOrder(Integer sourceDataId, Integer dataType, Integer dirOrder) {
		return getMapper().selectPOByDirOrder(sourceDataId, dataType, dirOrder);
	}

	public int updateByPrimaryKeySelective(XpathVariantRelPO record) {
		return getMapper().updateByPrimaryKeySelective(record);
	}

	public int updateByPrimaryKey(XpathVariantRelPO record) {
		return getMapper().updateByPrimaryKey(record);
	}

	/**
	 * 根据传入的Map条件进行查询，当前仅支持所有Map中Key字段的EqualTo查询
	 * 
	 * @param params
	 *            Map,Key=字段名，value=查询值
	 * @return
	 */
	public List<XpathVariantRelPO> selectByMap(Map<String, Object> params) {
		return (selectByArg(buildQueryObject(params)));
	}

	private XpathVariantRelArg buildQueryObject(Map<String, Object> params) {

		XpathVariantRelArg arg = new XpathVariantRelArg();
		XpathVariantRelCriteria criteria = arg.createCriteria();

		Class criteriaClass = criteria.getClass();
		Set keys = params.keySet();

		if (keys != null) {

			Iterator iterator = keys.iterator();

			while (iterator.hasNext()) {

				Object key = iterator.next();
				Object value = params.get(key);
				for (Method method : criteriaClass.getMethods()) {
					if (method.getName().equals("and" + StringUtils.toUpperCaseFirstOne(key.toString()) + "EqualTo")) {
						try {
							method.invoke(criteria, value);
						} catch (Exception e) {
							throw new SysRuntimeException(e);
						}
						break;
					}
				}
			}
		}
		return arg;
	}

	public IXpathVariantRelMapper getMapper() {
		return getSqlSession().getMapper(IXpathVariantRelMapper.class);
	}

}
